<?php


namespace app\api\logic;


use app\common\logic\BaseLogic;
use app\common\model\file\File;
use app\common\model\task\Task;
use app\common\service\ConfigService;
use think\facade\Db;

class TaskLogic extends BaseLogic
{
    /**
     * @notes 添加新数据1111
     * @return bool
     * @author ljj
     * @date 2022/2/24 10:52 上午
     */
    public function getdata($params)
    {
        ///启动事务
        Db::startTrans();
        try {
            //查询 file表的id
            $uri=$params['uri'];
            $dataUri=File::where(['uri'=>$uri])->findOrEmpty()->toArray();

            $data= Task::create([
                'source_id' => $params['user_id'],
                'file_id'=>$dataUri['id'],
                'uri' => $params['uri'],
                'text' => $params['text'],
                'address' => $params['address'] ,
                'longitude' => $params['longitude'] ,
                'latitude' => $params['latitude'] ,
            ]);
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            self::$error = $e->getMessage();
            return false;
        }
    }
    /**
     * @notes 坐标解析 路线规划
     * @param $get
     * @return array|mixed
     * @author ljj
     * @date 2022/10/13 2:44 下午
     * 本接口提供由经纬度到文字地址及相关位置信息的转换能力
     *
     *
     */
    public static function getaddress($get,$to)
    {
        $key = ConfigService::get('map', 'tencent_map_key','');
        if ($key == '') {
            return ['status'=>1,'message'=>'腾讯地图开发密钥不能为空'];
        }
        // 将to的坐标添加到$get数组中
        $get['to'] = $to;
        $get['key'] = $key;
        $query = http_build_query($get);
        $url = 'https://apis.map.qq.com/ws/direction/v1/driving/';
        $result =  json_decode(file_get_contents($url.'?'.$query), true);

        return $result;
    }

    /**
     * @notes 查询当前订单id的经纬度
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author ljj
     * @date 2022/2/21 6:12 下午
     */
    public function findlat($id)
    {
        $to = Task::field('latitude,longitude')
            ->where('id', '=', $id)
            ->find()
            ->toArray();

        return $to;
    }
}